﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Portal.Web.Model;
using Portal.Web.Utility;
using Portal.Exam.Bussiness;
using Portal.Exam.Model;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class MakeExamRoomStudent
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();

            string ExamRoomId = (string)WebEnvironments.Request(SiteId, "ExamRoomId");
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);


                ExamRoomCls OExamRoom = new ExamRoomBll().CreateModel(SiteId, ExamRoomId);
                if (OExamRoom == null) throw new Exception("Tham số không hợp lệ!");
                DataTable dtExamTemplate = new ExamTemplateBll().ReadingExamTemplate(SiteId, "");
                string SelectExamTemplate =
                    "<select id=drpSelectExamTemplate>\r\n";
                for (int iIndex = 0; iIndex < dtExamTemplate.Rows.Count; iIndex++)
                {
                    string ExamTemplateId = (string)dtExamTemplate.Rows[iIndex]["ExamTemplateId"];
                    string ExamTemplateName = (string)dtExamTemplate.Rows[iIndex]["ExamTemplateName"];
                    if (OExamRoom.ExamTemplateId.Equals(ExamTemplateId))
                    {
                        SelectExamTemplate += "   <option selected value=\"" + ExamTemplateId + "\">" + ExamTemplateName + "</option>\r\n";
                    }
                    else
                    {
                        SelectExamTemplate += "   <option value=\"" + ExamTemplateId + "\">" + ExamTemplateName + "</option>\r\n";
                    }
                }
                SelectExamTemplate +=
                    "</select>\r\n";
                string RetHtml =
                    " <script language=javascript> \r\n" +
                    " StartLine=0;\r\n" +
                    " MaxLine=0;\r\n" +

                    "function MakeExam(ExamRoomId)\r\n" +
                    " {\r\n" +
                    "    SiteId='" + SiteId + "';\r\n" +
                    "    ExamTemplateId=document.getElementById('drpSelectExamTemplate').value;\r\n" +
                    "    if(ExamTemplateId=='')\r\n" +
                    "    {\r\n" +
                    "       alert('Chưa xác định mẫu đề thi!');\r\n" +
                    "       return;\r\n" +
                    "    }\r\n" +
                    "    AjaxOut = Portal.Exam.Render.MakeExamRoomStudent.ServerSideDetectMax(SiteId, ExamRoomId,ExamTemplateId).value;\r\n" +
                    "    MaxLine=AjaxOut.RetNumber;\r\n" +
                    "    if(MaxLine<=0)\r\n" +
                    "    {\r\n" +
                    "       alert('Không có thí sinh nào trong phòng thi để tạo đề!');\r\n" +
                    "       return;\r\n" +
                    "    }\r\n" +
                    "    document.getElementById('txtIds').value=AjaxOut.HtmlContent;\r\n" +
                    "    StartLine=0;\r\n" +
                    "    document.getElementById('divMakeExamLog').innerHTML='';\r\n" +
                    "    document.getElementById('divMakeExamLog').style.display='block';\r\n" +
                    "    setTimeout('ProcessLine()',30);\r\n" +
                    " }\r\n" +


                    //"function MakeExam(ExamRoomId)\r\n" +
                    //" {\r\n" +
                    //"    SiteId='" + SiteId + "';\r\n" +
                    //"    ExamTemplateId=document.getElementById('drpSelectExamTemplate').value;\r\n" +
                    //"    if(ExamTemplateId=='')\r\n" +
                    //"    {\r\n" +
                    //"       alert('Chưa xác định mẫu đề thi!');\r\n" +
                    //"       return;\r\n" +
                    //"    }\r\n" +
                    //"    AjaxOut = Portal.Exam.Render.MakeExamRoomStudent.ServerSideDetectMax(SiteId, ExamRoomId,ExamTemplateId).value;\r\n" +
                    //"    MaxLine=AjaxOut.RetNumber;\r\n" +
                    //"    if(MaxLine<=0)\r\n" +
                    //"    {\r\n" +
                    //"       alert('Không có thí sinh nào trong phòng thi để tạo đề!');\r\n" +
                    //"       return;\r\n" +
                    //"    }\r\n" +
                    //"    document.getElementById('txtIds').value=AjaxOut.HtmlContent;\r\n" +
                    //"    StartLine=0;\r\n" +
                    //"    document.getElementById('divMakeExamLog').innerHTML='';\r\n" +
                    //"    document.getElementById('divMakeExamLog').style.display='block';\r\n" +
                    //"    setTimeout('ProcessLine()',30);\r\n" +
                    //" }\r\n" +

                    " function ProcessLine()\r\n" +
                    " {\r\n" +
                    "    if(StartLine==MaxLine)\r\n" +
                    "    {\r\n" +
                    "       StartLine=0;\r\n" +
                    "       MaxLine=0;\r\n" +
                    "       ProcessBarText = Portal.Exam.Render.MakeExamRoomStudent.ServerSideGetProcessBar(SiteId, MaxLine, MaxLine).value;\r\n" +
                    "       document.getElementById('divProcessing').innerHTML='';\r\n" +
                    "       document.getElementById('divMakeExamLog').innerHTML='Đã tạo xong đề cho các thí sinh trong phòng<br>'+MakeExamLog;\r\n" +
                    "    }\r\n" +
                    "    else\r\n" +
                    "    {\r\n" +
                    "       ProcessBarText = Portal.Exam.Render.MakeExamRoomStudent.ServerSideGetProcessBar(SiteId, StartLine, MaxLine).value;\r\n" +
                    "       document.getElementById('divProcessing').innerHTML=ProcessBarText;\r\n" +
                    "       ExamRoomId='" + ExamRoomId + "';\r\n" +
                    "       ExamTemplateId=document.getElementById('drpSelectExamTemplate').value;\r\n" +
                    "       Ids=document.getElementById('txtIds').value;\r\n" +
                    "       TotalTime=document.getElementById('txtTotalTime').value;\r\n" +
                    //"       NoOfQuestion=document.getElementById('txtNoOfQuestion').value;\r\n" +
                    "       NoOfQuestion='0';\r\n" +
                    "       ReCreateSession=document.getElementById('chkReCreateSession').checked;\r\n" +

                    "       TemplateConfig = '';\r\n" +
                    //"       if(MakeAll == 0)\r\n" +
                    //"       {\r\n" +

                    //"       Easy=document.getElementById('chk_Easy').checked;\r\n" +
                    //"       Medium=document.getElementById('chk_Medium').checked;\r\n" +
                    //"       Hard=document.getElementById('chk_Hard').checked;\r\n" +

                    //"       if(Easy == false && Medium==false && Hard == false)\r\n" +
                    //"       {\r\n" +                    
                    //"               alert('Bạn phải chọn và nhập tỉ lệ trước khi tạo đề!');\r\n" +
                    //"               return ;\r\n" +                    
                    //"       }\r\n" +

                    //"       NumEasy = 0;\r\n" +
                    //"       if(Easy == true)\r\n" +
                    //"       {\r\n" +
                    //"           if(document.getElementById('txt_Easy') != null)\r\n" +
                    //"           {\r\n" +
                    //"               NumEasy=document.getElementById('txt_Easy').value;\r\n" +
                    //"           }\r\n" +
                    //"       }\r\n" +

                    //"       NumMedium = 0;\r\n" +
                    //"       if(Medium == true)\r\n" +
                    //"       {\r\n" +
                    //"           if(document.getElementById('txt_Medium') != null)\r\n" +
                    //"           {\r\n" +
                    //"               NumMedium=document.getElementById('txt_Medium').value;\r\n" +
                    //"           }\r\n" +
                    //"       }\r\n" +


                    //"       NumHard = 0;\r\n" +
                    //"       if(Hard == true)\r\n" +
                    //"       {\r\n" +
                    //  "         if(document.getElementById('txt_Hard') != null)\r\n" +
                    //"           {\r\n" +
                    //"               NumHard=document.getElementById('txt_Hard').value;\r\n" +
                    //"           }\r\n" +
                    //"       }\r\n" +

                    //"       TemplateConfig = '0:' + NumEasy + ';' + '1:' + NumMedium + ';' + '2:' + NumHard;\r\n" +
                    //"       }\r\n" +


                    "       AjaxOut = Portal.Exam.Render.MakeExamRoomStudent.ServerSideProcessLine(SiteId, ExamRoomId, ExamTemplateId, Ids, TotalTime, StartLine, ReCreateSession,NoOfQuestion, TemplateConfig).value;\r\n" +
                    "       MakeExamLog=document.getElementById('divMakeExamLog').innerHTML;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           document.getElementById('divMakeExamLog').innerHTML=AjaxOut.InfoMessage+'<br>'+MakeExamLog;\r\n" +
                    "       }\r\n" +
                    "       StartLine=StartLine+1;\r\n" +
                    "       setTimeout('ProcessLine()',30);\r\n" +
                    "    }\r\n" +
                    " }\r\n" +
                    " </script>\r\n" +
                    " <div class=DatabaseFunction>\r\n" +
                    "        <ul>\r\n" +
                    //"            <li onclick=\"javascript:MakeExam('" + ExamRoomId + "');\"><img class=image src=\"" + IconRoot + "/StartWorkflow.png\"><div>Tạo toàn bộ</div></li>\r\n" +
                    "            <li onclick=\"javascript:MakeExam('" + ExamRoomId + "');\"><img class=image src=\"" + IconRoot + "/Execute.png\"><div>Thực hiện</div></li>\r\n" +
                    "            <li onclick=\"javascript:window.open('ExamRoomStudent.aspx?ExamRoomId=" + ExamRoomId + "','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                    "        </ul>\r\n" +
                    "  </div>\r\n" +
                    "  <input id=\"txtIds\" type=\"hidden\">\r\n" +
                    "  <h3>Tạo đề thi cho thí sinh trong phòng thi - " + OExamRoom.RoomNo + " / " + OExamRoom.ExamSubject + "</h3>\r\n" +

                    //"  <div class=CssFunc>Số câu hỏi: <input id=txtNoOfQuestion style=\"width:60px;\" value=\"10\">  /  Thời gian: <input id=txtTotalTime style=\"width:60px;\" value=\"60\"> phút  /  Khởi tạo lại: <input type=checkbox id=\"chkReCreateSession\"></div>\r\n" +
                    "  <div class=CssFunc>Mẫu đề thi: " + SelectExamTemplate + "  /  Thời gian: <input id=txtTotalTime style=\"width:60px;\" value=\"60\"> phút  /  Khởi tạo lại: <input type=checkbox id=\"chkReCreateSession\"></div>\r\n" +

                    "  <div class=CssTip>Trong trường hợp đã tạo đề thi nếu muốn tạo lại bàn cần tích vào nút <i>'Khởi tạo lại'</i></div>\r\n" +

                    " <div class=CssFormSaveUpdate>\r\n" +
                    //"   <table class=\"tableconfig\">" +
                    //"<tr class=\"tableheader\">" +
                    //"         <th>Mẫu đề thi</th>" +
                    //"         <th>Cấp độ câu hỏi</th>" +
                    //"         <th>Tỉ lệ %</th>" +
                    //"</tr>" +

                    //"       <tr>" +
                    //"           <td rowspan=3>" + SelectExamTemplate + "</td>" +
                    //"           <td rowspan=3>" +
                    //"               <input type=checkbox id='chk_Easy' value='0'>&nbsp Dễ</br>" +
                    //"               <input type=checkbox id='chk_Medium' value='1'>&nbsp Trung bình</br>" +
                    //"               <input type=checkbox id='chk_Hard' value='2'>&nbsp Khó" +
                    //"           </td>" +
                    //"           <td rowspan=3>" +
                    //"               <input type=text id='txt_Easy' value=0  class='textbox'></br>" +
                    //"               <input type=text id='txt_Medium' value=0  class='textbox'></br>" +
                    //"               <input type=text id='txt_Hard' value=0  class='textbox'>" +
                    //"           </td>" +
                    //"       </tr>" +

                    //"   </table>" +
                    " </div>\r\n" +

                    "  <div class=CssImportForm>\r\n" +
                    "       <div id=divProcessing></div>\r\n" +
                    "       <div id=\"divMakeExamLog\" style=\"margin-left:5px;margin-right:5px;margin-top:10px;margin-bottom:20px; -moz-border-radius: 7px; -webkit-border-radius: 7px; padding:4px;border:solid 1px silver;height:200px;overflow:auto;display:none;\"></div>\r\n" +
                    "       <input type=hidden id=txtImportFile value=\"\">\r\n" +

                    "       <div class=CssTip style=\"color:maroon;font-size:12px;\"><i>(*)Nếu mẫu đề thì trên không đủ câu hỏi lấy theo tỉ lệ được chọn hệ thống sẽ tự động gia tăng ngẫu nhiên các câu hỏi để bổ sung đủ số lượng câu hỏi</i></div>\r\n" +
                    "       <div class=CssTip style=\"color:maroon;font-size:12px;\"><i>(*)Chọn vào mẫu đề thi để sinh đề thi cho các thí sinh trong phòng thi</i></div>\r\n" +
                    "  </div>\r\n";

                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideDetectMax(string SiteId, string ExamRoomId, string ExamTemplateId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                DataTable dtExamRoomStudent = new ExamRoomStudentBll().ReadingExamRoomStudent(SiteId, ExamRoomId, null);
                string Ids = "";
                for (int iIndex = 0; iIndex < dtExamRoomStudent.Rows.Count; iIndex++)
                {
                    string ExamRoomStudentId = (string)dtExamRoomStudent.Rows[iIndex]["ExamRoomStudentId"];
                    Ids = Ids + ExamRoomStudentId + ";";
                }

                DataTable dtExamTemp = new ExamRoomStudentBll().ReadingQuestionTypeExamTemp(SiteId, ExamTemplateId, null);
                string ExamTempType = string.Empty;
                int roomStudent = 0;
                if (dtExamTemp.Rows[0]["Category"] != null)
                {
                    ExamTempType = dtExamTemp.Rows[0]["Category"].ToString();
                    if (dtExamRoomStudent.Rows.Count == 0)
                    {
                        if (ExamTempType.ToLower().Equals("freetext"))
                        {
                            roomStudent = 1;
                        }                        
                    }
                    else
                    {
                        roomStudent = dtExamRoomStudent.Rows.Count;
                    }
                }

                OAjaxOut.HtmlContent = Ids;
                OAjaxOut.RetNumber = roomStudent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static string ServerSideGetProcessBar(string SiteId, int ProcessLine, int MaxLine)
        {
            int MaxW = 700;
            decimal W = 0;
            decimal Percent = 0;
            if (MaxLine > 0)
            {
                W = (decimal)MaxW * (decimal)ProcessLine / (decimal)MaxLine;
                Percent = ((decimal)ProcessLine / (decimal)MaxLine) * 100;
            }
            if (Percent < 1) Percent = 1;
            if (Percent > 100) Percent = 100;
            string HtmlProgressBar = "<div style=\"width:" + MaxW + "px;padding:10px; background-color:silver;border:solid 1px black;height:55px;margin-top:10px;margin-bottom:10px;\">\r\n" +
                                     "   <div style=\"padding:4px;\">Xử lý dòng " + (ProcessLine + 1) + "/" + MaxLine + "</div>\r\n" +
                                     "   <div style='border:solid 1px black;padding:4px;background-color:blue;color:white;width:" + W.ToString("###0") + "px'>" + Percent.ToString("#,##0") + "% </div>\r\n" +
                                     "</div>\r\n";

            return HtmlProgressBar;
        }

        //NoOfQuestion
        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideProcessLine(
            string SiteId,
            string ExamRoomId,
            string ExamTemplateId,
            string Ids,
            string TotalExamTime,
            int ProcessLine,
            bool ReCreateSession,
            string NoOfQuestion,
            string TemplateConfig
            )
        {
            AjaxOut OAjaxOut = new AjaxOut();
            OAjaxOut.InfoMessage = "";
            try
            {
                string[] Data = Ids.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                string ExamRoomStudentId = Data[ProcessLine];
                if (ProcessLine == 0)
                {
                    new ExamRoomBll().SetExamTemplate(null, SiteId, ExamRoomId, ExamTemplateId);
                }
                new ExamRoomStudentBll().MakeExamByExamTemplate(null, SiteId, ExamTemplateId, ExamRoomStudentId, int.Parse(TotalExamTime), ReCreateSession, NoOfQuestion, TemplateConfig);
                OAjaxOut.InfoMessage = "";// "Dòng: " + (ProcessLine + 1) + " xử lý sinh viên mã [" + StudentCode + "] thành công!";
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = "Lỗi xử lý dòng " + (ProcessLine + 1) + " => " + ex.Message.ToString();
            }
            return OAjaxOut;
        }

        //[AjaxPro.AjaxMethod]
        //public static AjaxOut ServerSideProcessLine(
        //    string SiteId,
        //    string ExamRoomId,
        //    string ExamTemplateId,
        //    string Ids,
        //    string TotalExamTime,
        //    int ProcessLine,
        //    bool ReCreateSession)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    OAjaxOut.InfoMessage = "";
        //    try
        //    {
        //        string[] Data = Ids.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        //        string ExamRoomStudentId = Data[ProcessLine];
        //        if (ProcessLine == 0)
        //        {
        //            new ExamRoomBll().SetExamTemplate(null, SiteId, ExamRoomId, ExamTemplateId);
        //        }
        //        new ExamRoomStudentBll().MakeExam(null, SiteId, ExamTemplateId, ExamRoomStudentId, int.Parse(TotalExamTime), ReCreateSession);
        //        OAjaxOut.InfoMessage = "";// "Dòng: " + (ProcessLine + 1) + " xử lý sinh viên mã [" + StudentCode + "] thành công!";
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = "Lỗi xử lý dòng " + (ProcessLine + 1) + " => " + ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}


    }
}
